<template>
    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="120px" v-show="showSearch">
        <el-form-item label="编号" prop="num">
            <el-input v-model="queryParams.num" placeholder="请输入编号" clearable />
        </el-form-item>
        <el-form-item label="名称" prop="taskName">
            <el-input v-model="queryParams.taskName" placeholder="请输入名称" clearable />
        </el-form-item>
        <el-form-item label="审核结果" prop="checkStatus">
            <el-select v-model="queryParams.checkStatus" placeholder="请选择任务状态" clearable>
                <el-option v-for="item in [{label:'通过',value:'3'},{label:'不通过',value:'2'}]" :key="item.value" :label="item.label"
                    :value="item.value">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="提交人" prop="submitUserName">
            <el-input v-model="queryParams.submitUserName" placeholder="请输入提交人" clearable />
        </el-form-item>
        <el-form-item label="省" prop="provinceId">
            <el-select v-model="queryParams.provinceId" placeholder="请选择省" @change="chooseProvince" clearable>
                <el-option v-for="item in provinceData" :key="item.id" :label="item.name" :value="item.id">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="市" prop="cityId">
            <el-select v-model="queryParams.cityId" placeholder="请选择市" @change="chooseCity" clearable>
                <el-option v-for="item in cityData" :key="item.id" :label="item.name" :value="item.id">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="区/县" prop="areaId">
            <el-select v-model="queryParams.areaId" placeholder="请选择区/县" @change="chooseArea" clearable>
                <el-option v-for="item in areaData" :key="item.id" :label="item.name" :value="item.id">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="handleQuery">搜索</el-button>
            <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="dataList" stripe tooltip-effect="light">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="编号" align="center" prop="num" />
        <el-table-column label="名称" align="center" prop="name" width="255" show-overflow-tooltip />
        <el-table-column label="提交人" align="center" prop="submitUserName" />
        <el-table-column label="报告" align="center" prop="reportName" width="255" show-overflow-tooltip>
            <template #default="scope">
                <el-button type="text" @click="handleDownload(scope.row)">{{ scope.row.reportName }}</el-button>
            </template>
        </el-table-column>
        <el-table-column label="地区" align="center" prop="district" show-overflow-tooltip/>
        <el-table-column label="审核结果" align="center" prop="checkStatus">
            <template #default="scope">
                <span v-if="scope.row.checkStatus == '3'">通过</span>
                <el-button type="text" @click="handleNoPass(scope.row)" v-if="scope.row.checkStatus == '2'">不通过</el-button>
            </template>
        </el-table-column>
        <el-table-column label="审核时间" align="center" prop="checkTime" />
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.page" v-model:limit="queryParams.rows"
        @pagination="getList" />

    <!-- 查看原因对话框 -->
    <el-dialog v-model="noPassOpen" title="查看原因" width="900px" append-to-body>
        <p></p>
        <el-form :model="form"  label-width="150px">
            <el-form-item label="不通过原因" prop="rejectAdvice">
            <el-input v-model="form.rejectAdvice" type="textarea" maxlength="500" show-word-limit
              :autosize="{ minRows: 5, maxRows: 5}" placeholder="请输入不通过原因" disabled/>
            </el-form-item>
            <el-form-item label="附件" prop="attachmentId" v-if="form.fileList[0].fileName">
                <fileUploadNoPass ref="fileUpload" v-model="form.fileList"  :limit='1' :isShowTip="false" disabled></fileUploadNoPass>
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="noPassOpen = false">关 闭</el-button>
            </div>
        </template>
    </el-dialog>
</template>
  
<script setup>
import { getProvince, getChildArea } from "@/api/system/division";
import { getAllLists, } from "@/api/resultManage/reportReview.js"
import { getUserId } from "@/utils/auth";
import fileUploadNoPass from './FileUpload'
const { proxy } = getCurrentInstance();
const dataList = ref([]);
const ids = ref();
const loading = ref(false);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const showSearch = ref(true);
const noPassOpen = ref(false);
const provinceData = ref([]); //省
const cityData = ref([]); //市
const areaData = ref([]); //区、县

const data = reactive({
    queryParams: {
        page: 1,
        rows: 10,
        provinceId: undefined,
        cityId: undefined,
        areId: undefined,
        province: undefined,
        city: undefined,
        are: undefined,
        num: undefined,
        name: undefined,
        checkStatus: undefined,
        submitUserName: undefined,
    },
    form:{
        reason:undefined,
    }
})
const { queryParams, form } = toRefs(data)

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.page = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    queryParams.value.province = undefined,
    queryParams.value.city = undefined,
    queryParams.value.are = undefined,
    handleQuery();
}

/** 查询数据列表 */
function getList() {
    loading.value = true;
    getAllLists(queryParams.value).then(response => {
        dataList.value = response.data.rows ? response.data.rows : [];
        total.value = response.data.total;
        loading.value = false;
    });
}

// 单个下载文件
function handleDownload(row) {
    proxy.download("/reportCheck/downloadFile", {localReport:row.localReportName,reportSavePath:row.savePath }, row.reportName);
}

// 不通过理由
function handleNoPass(row) {
    form.value.rejectAdvice = row.rejectAdvice;
    form.value.fileList = [{
        fileName: row.rejectFile,
        savePath: row.savePath,
        realFileName:  row.rejectRealFile,
    }]
    noPassOpen.value = true;
}

/** 获取省列表 */
function getProvinceData() {
    getProvince().then((response) => {
        provinceData.value = response.data;
    });
}

function chooseProvince(value) {
    queryParams.value.city = null;
    queryParams.value.area = null;
    queryParams.value.cityId = null;
    queryParams.value.areaId = null;
    cityData.value = [];
    areaData.value = [];
    getChildArea(value).then(response => {
        cityData.value = response.data;
    });
    queryParams.value.province = provinceData.value.find(item => item.id == value)?.name;
}

function chooseArea(value) {
    queryParams.value.area = areaData.value.find(item => item.id == value)?.name;
}

function chooseCity(value) {
    queryParams.value.area = null;
    queryParams.value.areaId = null;
    areaData.value = [];
    getChildArea(value).then(response => {
        areaData.value = response.data;
    });
    queryParams.value.city = cityData.value.find(item => item.id == value)?.name
}


getProvinceData();
getList();
</script>
  
<style lang="scss" scoped>
.hsdTime {
    margin-right: 0px !important;

}
</style>